<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$print = isset($_GET['print']) ? $_GET['print'] : 0;
  $count = 0;
  $productMArray = array();
  $productDArray = 0;
  
  if(isset($_POST['invSubmit']))
  {
  	$proInvoiceDate      = $_POST['proInvoiceDateYear']."-".$_POST['proInvoiceDateMonth']."-".$_POST['proInvoiceDateDay']; 
  	$updateProMaster = "UPDATE proformamaster
  	                       SET status = 'D',
  	                           invoiceNo = '".$_POST['proInvoiceNo']."',
  	                           invoiceDate = '".$proInvoiceDate."',
  	                           userId = '".$s_activeUserId."' 
  	                     WHERE proformaMasterId = ".$_POST['proformaMasterId'];
    $updateProMasterRes = mysql_query($updateProMaster);
    if(!$updateProMasterRes)
      die("Update Query Not Fired : ".mysql_error());
    else
      header("Location:proformaListComplete.php");
  }
  
  
  
  $selectProduct = "SELECT proformamaster.proformaMasterId,DATE_FORMAT(proformamaster.date,'%d-%m-%Y')AS date,
                           party.companyName,deliverychallanmaster.srNo,proformamaster.totalAmt,proformamaster.notes,
                           proformamaster.status,proformamaster.invoiceNo,
                           DATE_FORMAT(proformamaster.invoiceDate,'%d-%m-%Y')AS invoiceDate
                      FROM proformamaster
                      JOIN deliverychallanmaster ON deliverychallanmaster.deliveryChallanMasterId = proformamaster.deliveryChallanMasterId
                      JOIN party ON party.partyId = deliverychallanmaster.partyId
                     WHERE proformamaster.proformaMasterId = ".$_GET['proformaMasterId'];
  $selectProductRes = mysql_query($selectProduct);
  while($row = mysql_fetch_array($selectProductRes))
  {
  	$productMArray['proformaMasterId'] = $row['proformaMasterId'];
  	$productMArray['date']             = $row['date'];
  	$productMArray['companyName']      = $row['companyName'];
  	$productMArray['srNo']             = $row['srNo'];
  	$productMArray['totalAmt']         = $row['totalAmt'];
  	$productMArray['notes']            = $row['notes'];
  	$productMArray['status']           = $row['status'];
  	$productMArray['invoiceNo']        = $row['invoiceNo'];
  	$productMArray['invoiceDate']      = $row['invoiceDate'];
  	
    $selProDtl = "SELECT itemName, qty,	unitRate,	amount,	itemText,	textAmt
  	                FROM proformadetail
  	                LEFT JOIN item ON proformadetail.itemId = item.itemId
  	               WHERE proformaMasterId = ".$_GET['proformaMasterId'];
    $selProDtlRes = mysql_query($selProDtl);
    $productDArray = array();
    $count = 0;
  	while($dtlRow = mysql_fetch_array($selProDtlRes))
  	{
  		if($dtlRow['itemName'] != NULL)
  		{
  		  $productDArray[$count]['itemName'] = $dtlRow['itemName'];
  		  $productDArray[$count]['qty']      = $dtlRow['qty'];
  		  $productDArray[$count]['unitRate'] = $dtlRow['unitRate'];
  		  $productDArray[$count]['amount']   = $dtlRow['amount'];
  		  $productDArray[$count]['itemText'] = $dtlRow['itemText'];
  		  $productDArray[$count]['textAmt']  = $dtlRow['textAmt'];
  		}
  		if($dtlRow['itemName'] == NULL && $dtlRow['itemText'] != '')
  		{
  			$productDArray[$count]['itemName'] = $dtlRow['itemText'];
  			$productDArray[$count]['qty']      = '';
  			$productDArray[$count]['unitRate'] = '';
  		}
  		if($dtlRow['amount'] == NULL && $dtlRow['textAmt'] != 0)
  		{
  			$productDArray[$count]['amount']   = $dtlRow['textAmt'];
  		}
  		$count++;
  	}
  }
	
  include("./bottom.php");
  $smarty->assign("productMArray",$productMArray);
  $smarty->assign("productDArray",$productDArray);
  
  if($print == 1)
  {
  	$smarty->display("proforDtlPrint.tpl");
  }
  elseif(isset($_GET['status']) && $_GET['status'] == 'D')
  {
  	$smarty->display("proformaStatus.tpl");
  }
  else
  {
    $smarty->display("proformaDetail.tpl");
  }
}
?>